package com.youzhi.backend.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.youzhi.backend.entity.RolePrivilege;
import com.youzhi.backend.mapper.RolePrivilegeMapper;
import com.youzhi.backend.service.IRolePrivilegeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 角色权限表 服务实现类
 * </p>
 *
 * @author xumeng
 * @since 2022-11-18
 */
@Service
public class RolePrivilegeServiceImpl extends ServiceImpl<RolePrivilegeMapper, RolePrivilege> implements IRolePrivilegeService {


    public void deleteByRoleIds(Collection roleIds) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.in("roleId", roleIds);
        remove(queryWrapper);
    }


    @Override
    public List<String> getPrivilegeCodes(Long roleId) {
        QueryWrapper<RolePrivilege> queryWrapper = new QueryWrapper();
        queryWrapper.select(RolePrivilege.Fields.privilegeCode);
        queryWrapper.select("privilegeCode");
        queryWrapper.eq("roleId", roleId);
        List<RolePrivilege> list = list(queryWrapper);
        if (CollUtil.isNotEmpty(list)) {
            return list.stream().map(e -> e.getPrivilegeCode()).collect(Collectors.toList());
        }
        return new ArrayList<>();
    }


    public Set<String> getPrivilegeCodes(Collection roleIds) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.select("privilegeCode");
        queryWrapper.in("roleId", roleIds);
        List<RolePrivilege> rolePrivileges = list(queryWrapper);
        return rolePrivileges.stream().map(e -> e.getPrivilegeCode()).collect(Collectors.toSet());
    }


}
